using micromsg;
using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Network;
using MicroMsg.Protocol;
using MicroMsg.Storage;
using System;
namespace MicroMsg.Scene
{
	public class NetSceneBindQQ : NetSceneBaseEx<BindQQRequest, BindQQResponse, BindQQRequest.Builder>
	{
		private const string TAG = "NetSceneBindQQ";
		public const int MM_BQOC_BINDQQ = 1;
		public const int MM_BQOC_GETVERIFYIMG = 2;
		public const int MM_BQOC_GENKEY = 3;
		private uint mBindUin;
		private string mPwd;
		public void doSceneBindQQ(uint bindUin, string string_0)
		{
			Log.smethod_4("NetSceneBindQQ", "bindQQ begin , QQ = " + bindUin);
			this.mBindUin = bindUin;
			this.mPwd = string_0;
			this.doSceneEx(bindUin, string_0, "", "", 1u);
		}
		public void doSceneBindQQWithVerify(string imgSid, string imgCode)
		{
			Log.smethod_4("NetSceneBindQQ", string.Concat(new object[]
			{
				"bindQQ with sid , QQ = ",
				this.mBindUin,
				", sid = ",
				imgSid
			}));
			this.doSceneEx(this.mBindUin, this.mPwd, imgSid, imgCode, 1u);
		}
		public void doSceneRefetchVerifyImgForBindQQ()
		{
			Log.smethod_4("NetSceneBindQQ", "bindQQ refetch verify image , QQ = " + this.mBindUin);
			this.doSceneEx(this.mBindUin, this.mPwd, "", "", 2u);
		}
		public void doSceneVerifyQQPwd(string string_0)
		{
			uint nBindQQ = AccountMgr.getCurAccount().nBindQQ;
			this.mBindUin = nBindQQ;
			this.mPwd = string_0;
			this.doSceneEx(nBindQQ, string_0, "", "", 3u);
		}
		public void doSceneVerifyQQPwdWithVerify(string imgSid, string imgCode)
		{
			uint nBindQQ = AccountMgr.getCurAccount().nBindQQ;
			this.doSceneEx(nBindQQ, this.mPwd, imgSid, imgCode, 3u);
		}
		private void doSceneEx(uint bindUin, string string_0, string imgSid, string imgCode, uint opCode)
		{
			base.beginBuilder();
			this.mBuilder.BaseRequest = NetSceneBase.makeBaseRequest(0);
			this.mBuilder.QQ = bindUin;
			this.mBuilder.Pwd = Util.getCutPasswordMD5(string_0);
			this.mBuilder.Pwd2 = Util.getFullPasswordMD5(string_0);
			this.mBuilder.ImgSid = imgSid;
			this.mBuilder.ImgCode = imgCode;
			this.mBuilder.OPCode = opCode;
			this.mSessionPack.mConnectMode = 2;
			this.mSessionPack.mCmdUri = "/cgi-bin/micromsg-bin/bindqq";
			base.endBuilder();
		}
		protected override void onFailed(BindQQRequest request, BindQQResponse response)
		{
			EventCenter.postEvent(EventConst.ON_NETSCENE_BINDQQ_ERR, -800000, null);
		}
		protected override void onSuccess(BindQQRequest request, BindQQResponse response)
		{
			RetConst ret = (RetConst)response.BaseResponse.Ret;
			uint oPCode = request.OPCode;
			if (3u == oPCode)
			{
				Log.smethod_5("NetSceneBindQQ", "verify qq pwd return.");
			}
			else
			{
				if (2u == oPCode)
				{
					Log.smethod_5("NetSceneBindQQ", "bindQQ get verifyimg return.");
				}
				else
				{
					if (1u != oPCode)
					{
						Log.smethod_1("NetSceneBindQQ", "bindQQ return for unknown op.");
						return;
					}
					Log.smethod_5("NetSceneBindQQ", "bindQQ return.");
				}
			}
			if (ret == RetConst.MM_OK)
			{
				if (3u == oPCode)
				{
					Log.smethod_5("NetSceneBindQQ", "Verify QQ PWD success. QQ = " + request.QQ);
				}
				else
				{
					Log.smethod_5("NetSceneBindQQ", "bindQQ success. QQ = " + request.QQ);
					Account curAccount = AccountMgr.getCurAccount();
					curAccount.nBindQQ = request.QQ;
					curAccount.bytesA2Key = response.A2Key.Buffer.ToByteArray();
					AccountMgr.updateAccount();
				}
				EventCenter.postEvent(EventConst.ON_NETSCENE_BINDQQ_SUCCESS, null, null);
				return;
			}
			if (ret == RetConst.MM_ERR_NEED_VERIFY)
			{
				Log.smethod_5("NetSceneBindQQ", string.Concat(new object[]
				{
					"bindQQ need verify, QQ = ",
					request.QQ,
					", sid = ",
					response.ImgSid
				}));
				EventCenter.postEvent(EventConst.ON_NETSCENE_BINDQQ_NEEDVERIFY, new VerifyCodeArgs
				{
					mImageSid = response.ImgSid,
					mImageBuf = response.ImgBuf.Buffer.ToByteArray()
				}, null);
				return;
			}
			if (ret == RetConst.MM_ERR_PASSWORD)
			{
				Log.smethod_5("NetSceneBindQQ", "QQ PWD verify, pwd err, QQ = " + request.QQ);
				EventCenter.postEvent(EventConst.ON_NETSCENE_BINDQQ_ERR, ret, null);
				return;
			}
			Log.smethod_5("NetSceneBindQQ", "bindQQ failed,   ret = " + ret);
			EventCenter.postEvent(EventConst.ON_NETSCENE_BINDQQ_ERR, ret, null);
		}
		private static void onHandler(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs.mEventID == EventConst.ON_NETSCENE_BINDQQ_SUCCESS)
			{
				Log.smethod_5("NetSceneBindQQ", "(2/2)on success");
				return;
			}
			if (evtArgs.mEventID == EventConst.ON_NETSCENE_BINDQQ_NEEDVERIFY)
			{
				Log.smethod_5("NetSceneBindQQ", "(1/2)on success");
				ServiceCenter.sceneBindQQ.doSceneBindQQWithVerify("test", "test");
			}
		}
		public static void testBindQQ()
		{
			EventWatcher eventWatcher = new EventWatcher(null, null, new EventHandlerDelegate(NetSceneBindQQ.onHandler));
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_BINDQQ_SUCCESS, eventWatcher);
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_BINDQQ_NEEDVERIFY, eventWatcher);
			ServiceCenter.sceneBindQQ.doSceneBindQQ(1838517202u, "bindqq222");
		}
	}
}
